package com.easymusic;

import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.MediaStore;

public class EasyMusicMedia {
	private Bitmap m_image;
	private String m_name;
	
	public EasyMusicMedia(Context context, String strMediaPath){
		setMediaImage(context, strMediaPath);
		int iIndexLastSlash = strMediaPath.lastIndexOf('/');
		int iIndexLastDot = strMediaPath.lastIndexOf('.');
		m_name = strMediaPath.substring(iIndexLastSlash + 1, iIndexLastDot);
	}
	
	public Bitmap getImage(){
		return m_image;
	}
	
	public String getName(){
		return m_name;
	}
	
	private void setMediaImage(Context context, String strMediaPath){
		Uri uriMedia = null;
		Uri uriAlbum = null;
		if (strMediaPath.indexOf("/mnt/extSdCard") == 0){
			uriMedia = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
			uriAlbum = MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI;
		}else{
			uriMedia = MediaStore.Audio.Media.INTERNAL_CONTENT_URI;
			uriAlbum = MediaStore.Audio.Albums.INTERNAL_CONTENT_URI;
		}
		String[] projection = {MediaStore.Audio.Media.DATA, MediaStore.Audio.Media.ALBUM_ID};
		Cursor c = context.getContentResolver().query(uriMedia, projection, null, null, null);
		if (null == c){
			return;
		}
		for (c.moveToFirst(); !c.isLast(); c.moveToNext()){
			String strPath = c.getString(c.getColumnIndex(MediaStore.Audio.Media.DATA));
			if (strPath.compareTo(strMediaPath) == 0){
				break;
			}
		}
		if (c.isLast()){
			return;
		}
		
		int iAlbumId = c.getInt(c.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM_ID));
		
		String[] projectionAlbum = {MediaStore.Audio.Albums.ALBUM_ART};
		uriAlbum = uriAlbum.parse(uriAlbum.toString() + "/" + iAlbumId);
		c = context.getContentResolver().query(uriAlbum, projectionAlbum, null, null, null);
		if (null != c && c.getCount() > 0){
			c.moveToNext();
			String str = c.getString(0);
			if (null != str){
				m_image = BitmapFactory.decodeFile(str);  
			}
		}
	}
	
}



































